<?php
class data extends db 
{
	public function __construct()
	{
		parent::__construct();
		
	}
	
	public function getAllIstanceCombo()
	{
		$this->query('SELECT istancename,  istanceid FROM istance');
		while(list($istancename, $istanceid) = $this->fetch_row())
		{
			$data[$istanceid] = $istancename;
		}
		
		return $data;
	}
	
	public function getRoleName($roleid)
	{
		$this->query("SELECT rolename FROM role WHERE roleid = '$roleid'");
		list($rolename) = $this->fetch_row();
		return $rolename;
	}
	
	public function getRoleID($rolename)
	{
		$this->query("SELECT roleid FROM role WHERE rolename = '$rolename'");
		list($roleid) = $this->fetch_row();
		return $roleid;
	}
	
	public function getRoleIDNull()
	{
		$this->query("SELECT roleid FROM role WHERE rolenull=1");
		list($roleid) = $this->fetch_row();
		return $roleid;
	}
	
	public function getRoleNameNull()
	{
		$this->query("SELECT rolename FROM role WHERE rolenull=1");
		list($rolename) = $this->fetch_row();
		return $rolename;
	}
	
	public function getAllRoleClean()
	{
		$this->query("SELECT roleid, rolename FROM role WHERE rolenull != 1 ORDER BY orders ASC");
		return $this->fetch_all_row();
	}
	
	public function getStatusOut($onlyid = false)
	{
		if($onlyid)
		{
			$this->query("SELECT signupstatusid, signupstatusvalue FROM signupstatus WHERE signupstatusoutlevel = 1");
			return $this->fetch_array();
		}
		else 
		{
			$this->query("SELECT signupstatusid FROM signupstatus WHERE signupstatusoutlevel = 1");
			list($signupid) = $this->fetch_row();
			return $signupid;
		}
	}
	
	public function getStatusConfirmed($onlyid = false)
	{
		if($onlyid)
		{
			$this->query("SELECT signupstatusid, signupstatusvalue FROM signupstatus WHERE signupstatusconfirmedlevel = 1");
			return $this->fetch_array();
		}
		else
		{
			$this->query("SELECT signupstatusid FROM signupstatus WHERE signupstatusconfirmedlevel = 1");
			list($signupid) = $this->fetch_row();
			return $signupid;
		}
	}
	
	public function getStatusQueue($onlyid = false)
	{
		if($onlyid)
		{
			$this->query("SELECT signupstatusid, signupstatusvalue FROM signupstatus WHERE signupstatusqueuelevel = 1");
			return $this->fetch_array();	
		}
		else
		{
			$this->query("SELECT signupstatusid FROM signupstatus WHERE signupstatusqueuelevel = 1");
			list($signupid) = $this->fetch_row();
			return $signupid;
		}

	}
	
	public function getIsYourPg(){
		$this->query("SELECT userid FROM signup WHERE signupid={$_POST['signupid']}");
		list($userid) = $this->fetch_row();
		if($userid == $_SESSION['userid'])
			RETURN "TRUE";
		else
			RETURN "FALSE";
	}
	
	public function getIsHaveSecondRole()
	{
		$this->query("SELECT pgid FROM signup WHERE signupid = {$_POST['signupid']}",0,1);
		list($pgid) = $this->fetch_row();
		$this->query("SELECT roleid FROM pgrole WHERE pgid=$pgid and main=0",0,1);
		if($this->num_rows())
			return "TRUE";
		else
			return "FALSE";
	}
	
	public function getStatusAll()
	{
		$profiles = profiles::getProfilesIstance();
		if(!$profiles->permission('_user_can_view_signup_confirmed'))
		{
			$query = "WHERE signupstatusconfirmedlevel = 0";
		}
		$this->query("SELECT signupstatusid, signupstatusvalue FROM signupstatus $query");
		while(list($signupstatusid, $signupstatusvalue) = $this->fetch_row())
			$data[$signupstatusid] = $signupstatusvalue;
		
		return $data;
	}
	
	public function getAllRaidCombo()
	{
		$raid = $this->query("SELECT raidid as id, raidname, raidmaxpg FROM raid");
		while(list($id, $name, $maxpg) = $this->fetch_row($raid))
		{
			$raidSetupArray[$id] = $name."(";
			$setup = $this->query("SELECT r.rolename, s.max FROM raidsetup s, role r WHERE r.roleid = s.roleid and s.raidid=$id");
			while(list($rolename, $maxpg) = $this->fetch_row($setup))
			{
				$raidSetupArray[$id] .= "[".$maxpg."]";
			}
			$raidSetupArray[$id] .= ")";
		}

		
		return $raidSetupArray;
	}
	
	public function getExistsCensedPgFromUser()
	{
			$this->query("SELECT * FROM pg WHERE userid = '{$_SESSION['userid']}'");
			return $this->num_rows();
	}
	
	
	public function getEventData($id)
	{
		$this->query("SELECT * FROM event WHERE eventid= $id");
		return $this->fetch_array();
	}
	
	public function getSetupRaidData($raidid)
	{
		$this->query("SELECT * FROM raid WHERE raidid = $raidid");
		return $this->fetch_array();
	}
	
	public function getIstanceData($istanceid)
	{
		$this->query("SELECT * FROM istance WHERE istanceid = $istanceid");
		return $this->fetch_array();
	}
		
	public function getAllSignupStatus()
	{
		$this->query("SELECT * FROM signupstatus");
		return $this->fetch_all_row();
	}
	
	public function getSignupStatus($idSignupStatus)
	{
		
	}
	
	public function geIstanceStatisticData($istanceid)
	{
		$this->query("SELECT count(*) FROM boss WHERE istanceid = '$istanceid'");
		list($array['bosscount']) = $this->fetch_row();
		$this->query("SELECT count(*) FROM boss WHERE istanceid = '$istanceid' and bossstatus = 'Ucciso'");
		list($array['bosskill']) = $this->fetch_row();
		$this->query("SELECT count(*) FROM event WHERE istanceid = $istanceid");
		list($array['istanceevent']) = $this->fetch_row();
		$this->query("SELECT count(*) FROM event");
		list($array['countevent']) = $this->fetch_row();
		$this->query("SELECT u.username, count(*) FROM 
			(guildprogress.pg p inner join signup s on (p.pgid=s.pgid)) inner join event e on (s.eventid=e.eventid) inner join user u on (s.userid=u.userid)
				where e.istanceid=$istanceid
				group by s.userid
				order by count(*) desc limit 1");
		list($array['usertop'], $array['usertopcount']) = $this->fetch_row();
		$this->query("SELECT p.pgname, count(*) FROM 
			(guildprogress.pg p inner join signup s on (p.pgid=s.pgid)) inner join event e on (s.eventid=e.eventid)
				where e.istanceid=$istanceid
				group by s.pgid
				order by count(*) desc limit 1");
		list($array['playertop'],$array['playertopcount']) = $this->fetch_row();
		$this->query("SELECT SUM((enddate-startdate)/60/60) FROM event WHERE istanceid='$istanceid'");
		list($array['istancegametime']) = $this->fetch_row();
		settype($array['istancegametime'],"integer");
		return $array;
	}
	
	public function getSignupIstanceStatistic($eventid)
	{
		$this->query("SELECT s.signuprole, count(signuprole) FROM signup s WHERE s.signupconfirmed=1 and s.eventid=$eventid  GROUP by s.signuprole",0,1);
		while(list($role,$count) = $this->fetch_row()){
			$array['confirmed'][$role] = $count;
		}
		$this->query("SELECT  s.signuprole, count(signuprole) FROM signup s WHERE s.signupconfirmed=0 and s.eventid=$eventid  GROUP by s.signuprole",0,1);
		while(list($role,$count) = $this->fetch_row()){
			$array['queue'][$role] = $count;
		}
		return $array;
	}
}

include_once 'data/boss.data.php';
include_once 'data/calendar.data.php';
include_once 'data/config.data.php';
include_once 'data/dkp.data.php';
include_once 'data/event.data.php';
include_once 'data/istance.data.php';
include_once 'data/log.data.php';
include_once 'data/loot.data.php';
include_once 'data/panel.data.php';
include_once 'data/pg.data.php';
include_once 'data/profile.data.php';
include_once 'data/progress.data.php';
include_once 'data/raid.data.php';
include_once 'data/signup.data.php';
include_once 'data/user.data.php';

?>